package logic;
import gui.GuiMain;

import java.util.ArrayList;

import memory.GenericEvents;

public class Display {
	private static final String BRACKET_CLOSE = "] ";
	private static final String BRACKET_OPEN = ".	[";
	private static final String BLANK = " ";
	private static final String ALERT = "!";
	private static final String MARK = "x";
	private static final String ITEM_VIEW = "Item View";
	private static final String DISPLAY_ITEM_TYPE = "Display item type: ";
	private static final String LINE2 = "_________________________________________________________________";
	private static final String LINE = "_________________________________________________________________\n";
	public static GenericEvents detailedView = null;
	public static ArrayList<GenericEvents> listView = null;
	public static String listName = null;

	//@author A0096677H
	// Clears all main window text
	public static void displayClear() {
		detailedView = null;
		listView = null;
		GuiMain.mainViewTextArea.setText("");
	}

	//@author A0096677H
	// Displays an individual GenericEvents item with all non-null details
	public static void displayDetailed(GenericEvents item) {
		displayClear();
		listView = null;
		detailedView = item;

		assert detailedView != null;

		formatItem(item);
	}

	//@author A0096677H
	// Formats the item display
	private static void formatItem(GenericEvents item) {
		System.out.println(ITEM_VIEW);
		System.out.println(LINE);
		System.out.println(item);
		System.out.println(LINE2);
	}

	//@author A0096677H
	// Takes an ArrayList of GeneticEvents items, and display them in a list form, with name as the heading
	public static void displayList(ArrayList<GenericEvents> list, String name) {
		displayClear();
		detailedView = null;
		listView = list;
		listName = name;

		assert listName != null;

		formatList(list);
	}

	//@author A0096677H
	// Formats the list display
	private static void formatList(ArrayList<GenericEvents> list) {
		System.out.println(listName);
		System.out.println(LINE);
		for (int i=0; i < list.size(); i++) {
			String line = i + 1 + BRACKET_OPEN;
			line = markSymbol(list, i, line);
			line += BRACKET_CLOSE + list.get(i).getName();

			System.out.println(line);
		}
		System.out.println(LINE2);
	}

	//@author A0096677H
	// Selects and prints the appropriate marking symbol
	private static String markSymbol(ArrayList<GenericEvents> list, int i,
			String line) {
		if (list.get(i).getState() == 1) { // show marking for overdue/over items
			line += MARK;
		} else if (list.get(i).getState() == 2) { // show alert marking for overdue/ongoing items
			line += ALERT;
		} else { // show blank space for upcoming/incomplete items
			line+= BLANK;
		}
		return line;
	}

	//@author A0096677H
	// Displays list of item with the corresponding type
	public static void displayType(ArrayList<GenericEvents> list, String type) {
		assert type.length() != 0;

		if (list.size() > 1) { // if list contains multiple items, show as list
			displayList(list, DISPLAY_ITEM_TYPE + type);
		} else if (list.size() == 1){ // if only one item, show item
			displayDetailed(list.get(0));
		}
	}

	//@author A0096677H
	// Redisplays item view or list view to reflect changes such as updates, marks, or deletes
	public static void refreshDisplay() {
		GuiMain.mainViewTextArea.setText("");
		if (listView != null) { // if previously in list view, redisplay list
			displayList(listView, listName);
		} else if (detailedView != null) { // if previously in item view, redisplay item
			displayDetailed(detailedView);
		}
	}

}